package digitalmat.p.com.google.code;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.AffineTransform;

import javax.swing.JComponent;


public class DrawPanel extends JComponent {

   private AffineTransform transform = AffineTransform.getScaleInstance(0.5, 0.5);

   public DrawPanel() {

      }


   protected AffineTransform getTransform() {
      return this.transform;
      }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(128, 128);
      }

   public Point canvas2view(int x, int y) {
      Point p = new Point((int) (x / (this.transform.getScaleX() * 16)),
                          (int) (y / (this.transform.getScaleY() * 16)));
      return p;
      }


   @Override
   public void paintComponent(Graphics g) {

      Graphics2D g2 = (Graphics2D) g;

      int unit = 160;


      g2.scale(this.transform.getScaleX(), this.transform.getScaleY());

      g2.setColor(new Color(0, 128, 0));

      g2.fillRect(0, 0, 4 * unit, 4 * unit);

      g2.setColor(Color.WHITE);
      g2.drawLine(0, 0, 4 * unit, 0);
      g2.drawLine(0, unit, 4 * unit, unit);
      g2.drawLine(0, 2 * unit, 4 * unit, 2 * unit);
      g2.drawLine(0, 3 * unit, 4 * unit, 3 * unit);
      g2.drawLine(0, 4 * unit, 4 * unit, 4 * unit);

      g2.drawLine(0, 0, 0, 4 * unit);
      g2.drawLine(unit, 0, unit, 4 * unit);
      g2.drawLine(2 * unit, 0, 2 * unit, 4 * unit);
      g2.drawLine(3 * unit, 0, 3 * unit, 4 * unit);
      g2.drawLine(4 * unit, 0, 4 * unit, 4 * unit);

      g2.scale(1 / this.transform.getScaleX(), 1 / this.transform.getScaleY());
      }

   }
